﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using CKFinder;

public partial class Admin_EditHDSD : System.Web.UI.Page
{
    HDSudungController hdSudungController = new HDSudungController();
    private string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        ckeditor_faq.config.toolbar = "Full";
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor_faq);
        if (!IsPostBack)
        {
            id = Request.QueryString["id"];
            if (id == "" || id == null || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {

                error();
            }
            else
            {

                int idd = Convert.ToInt32(id);
                if (!this.checkId(idd))
                {
                    error();
                }
                else
                {
                    loadData(idd);
                }

            }
        }
    }
    private static void error()
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
    private bool checkId(int id)
    {
        DataTable dt = hdSudungController.GetByID(id);

        if (dt.Rows.Count > 0)
        {
            return true;
        }
        return false;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HDSudung hd = new HDSudung();
        id = Request.QueryString["id"];
        int idd = Convert.ToInt32(id);
        hd.HD_Sudung_id =idd;
        hd.Title = txtTitle.Text;
        hd.Content = ckeditor_faq.Text;
        hd.DateUpdate = DateTime.Now;
        hd.Url = txtUrl.Text;
        if (ddlStatus.SelectedIndex == 0)
        {
            hd.Status = true;
        }
        else
        {
            hd.Status = false;
        }
        if (hdSudungController.Update(hd) > 0)
        {
            Response.Write("<script>alert('Cập nhật hướng dẫn sử dụng thành công.')</script>");
            loadData(idd);
        }
        else
        {
            Response.Write("<script>alert('Cập nhật hướng dẫn sử dụng gặp lỗi.')</script>");
            return;
        }
    }
    public void loadData(int idd)
    {
        DataTable dt = hdSudungController.GetByID(idd);
        HDSudung hd = new HDSudung(dt.Rows[0]);
        ckeditor_faq.Text = hd.Content;
        txtTitle.Text = hd.Title;
        txtUrl.Text = hd.Url;

        if (hd.Status)
        {
            ddlStatus.SelectedIndex = 0;
        }
        else
        {
            ddlStatus.SelectedIndex = 1;
        }
    }
}